﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace AIFast.Lakala
{
    /// <summary>
    /// 拉卡拉开放平台参数
    /// </summary>
    public class LakalaOpenOptions
    {
        public string NativeUrl { get; set; } 

        public string LocalUrl { get; set; } 

        public string AppId { get; set; }
        /// <summary>
        /// 正式序列号
        /// </summary>
        public string SerialNo { get; set; }

        public string Sm4Key { get; set; }

        public string PlatformId { get; set; }

        public string BindOrgCode { get; set; }

        public string PubCert { get; set; }

        public string PriPem { get; set; }

        private static RSA _priRsa;
        private static RSA _pubRsa;

        public RSA GetPubRsa()
        {
            if (_pubRsa == null)
            {
                lock (PubCert)
                {
                    if (_pubRsa == null)
                    {
                        var cert = new X509Certificate2(Encoding.UTF8.GetBytes(PubCert));
                        _pubRsa = cert.GetRSAPublicKey();
                    }
                }
            }

            return _pubRsa;
        }

        public RSA GetPriRsa()
        {
            if (_priRsa == null)
            {
                lock (PriPem)
                {
                    if (_priRsa == null)
                    {
                        _priRsa = new RSACryptoServiceProvider();
                        _priRsa.ImportFromPem(PriPem);
                    }
                }
            }

            return _priRsa;
        }
    }
}
